package homework;

/**
 * 3)、模拟三个老师同时分发30份笔试试卷，每个老师相当于一个线程。
 */
class Teacher implements Runnable{

    private static  int ticked = 30;
    private Object obj = new Object();
    @Override
    public void run() {
        while (true) {
            synchronized (/*Teacher.class/this 也可以*/  obj) {
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if (ticked > 0) {
                    System.out.println(Thread.currentThread().getName() + "在发试卷！ " +
                            "还有" + ticked + "份试卷！");
                    ticked--;
                } else {
                    break;
                }
            }
        }
    }
}



public class No_3 {


    public static void main(String[] args) {
        Teacher teacher = new Teacher();
        Thread t1 = new Thread(teacher);
        Thread t2 = new Thread(teacher);
        Thread t3 = new Thread(teacher);
        t1.setName("由老师 1 ");
        t2.setName("由老师 2 ");
        t3.setName("由老师 3 ");
        t1.start();
        t2.start();
        t3.start();
    }

}
